package com.woniu.woniu_bx.controller;

import com.woniu.woniu_bx.pojo.Manager;
import com.woniu.woniu_bx.pojo.Menu;
import com.woniu.woniu_bx.pojo.Perm;
import com.woniu.woniu_bx.pojo.ResponseEntity;
import com.woniu.woniu_bx.service.ManagerService;
import com.woniu.woniu_bx.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author : 王泰元
 * @className: MenuController
 * @description: TODO
 * @date : 2022/8/30 14:52
 */
@RestController
@RequestMapping("/admin")
@Slf4j
public class MenuController {
    @Autowired
    private ManagerService managerService;
    @Autowired
    private MenuService menuService;

    @GetMapping("/allMenus")
    public ResponseEntity getMenus(String account) {
        // 根据管理员角色查询菜单
        Manager admin = managerService.getManagerByAccount(account);
        List<Menu> menuList = menuService.getMenuListByRole(admin.getRoleId());
        List<Menu> pmenuList = menuList.stream().filter(o -> o.getPid() == null).collect(Collectors.toList());
        menuList.removeAll(pmenuList);
        for (Menu menu : pmenuList) {
            List<Menu> list = menuList.stream().filter(o -> menu.getId().equals(o.getPid())).collect(Collectors.toList());
            menu.setSubMenu(list);
        }
        return ResponseEntity.SUCCESS("ok", pmenuList);
    }


    @GetMapping("/findMenus/{id}")
    public ResponseEntity findMenusByRoleId(@PathVariable int id){
        List<Menu> menusList = menuService.findMenusByRoleId(id);
        return ResponseEntity.SUCCESS("查找菜单列表成功",menusList);
    }

    @GetMapping("/menusList")
    public ResponseEntity menusList(){
        List<Menu> menusList = menuService.menusList();
        return ResponseEntity.SUCCESS("查找菜单列表成功",menusList);
    }
}
